package com.tang.algorithm.swordFingerOffer;

/**
 * @author tlm
 * @version 1.0
 * @date 2020/5/4 19:55
 * 反转链表
 * https://leetcode-cn.com/problems/fan-zhuan-lian-biao-lcof/
 */
public class ReverseLinkedList {
    public static void main(String[] args) {
        ListNode listNode1 = new ListNode(1);
        ListNode listNode2 = new ListNode(2);
        ListNode listNode3 = new ListNode(3);
        ListNode listNode4 = new ListNode(4);
        ListNode listNode5 = new ListNode(5);
        listNode1.next = listNode2;
        listNode2.next = listNode3;
        listNode3.next = listNode4;
        listNode4.next = listNode5;
        ListNode temp = reverseList(listNode1);
        System.out.println(temp.val);
    }
    public static ListNode reverseList(ListNode head) {
        //利用双指针
        ListNode former = head;
        ListNode last = null;

        while (former!=null){
            ListNode temp = former.next;
            former.next = last;
            last = former;
            former = temp;
        }
        return last;
    }
}
